<?php

namespace App\Admin\Controllers;

use App\Models\CommonRegion;
use App\Models\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;


class UserController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '平台用户';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {


        $grid = new Grid(new User());

        $grid->column('id',  ___('table.zy_user.id'));
        $grid->column('phone',  ___('table.zy_user.phone'));
        //$grid->column('password',  ___('table.zy_user.password'));
        $grid->column('icon',  ___('table.zy_user.icon'))->image('',40,40)->width(100);
        $grid->column('nickname',  ___('table.zy_user.nickname'));
        $grid->column('remark',  ___('table.zy_user.remark'));
        $grid->column('gender',  ___('table.zy_user.gender'))->using(['2' => '女', '1' => '男']);;
        //$grid->column('birthday',  ___('table.zy_user.birthday'));
        $grid->column('age','年龄');
        $grid->column('email',  ___('table.zy_user.email'));
        $grid->column('province_id', ___('table.zy_user.Province id'))->display(function ($id){
            $region = CommonRegion::find($id);
            if ($region) {
                return $region->name;
            }
            return '地区未配置';
        });
        $grid->column('city_id', ___('table.zy_user.City id'))->display(function ($id){
            $region = CommonRegion::find($id);
            if ($region) {
                return $region->name;
            }
            return '地区未配置';
        });
        $grid->column('district_id', ___('table.zy_user.District id'))->display(function ($id){
            $region = CommonRegion::find($id);
            if ($region) {
                return $region->name;
            }
            return '地区未配置';
        });
        $grid->column('name',  ___('table.zy_user.name'));
        $grid->column('authentication',  ___('table.zy_user.authentication'))->display(function ($auth){
            return $auth?"Y":"N";
        });
        $grid->column('integral',  ___('table.zy_user.integral'));
        $grid->column('balance',  ___('table.zy_user.balance'));
        $grid->column('status', ___('table.zy_user.status'))->icon([
            0 => 'toggle-off',
            1 => 'toggle-on',
        ], $default = '');
        $grid->column('login_time',  ___('table.zy_user.login_time'));
        //$grid->column('create_time',  ___('table.zy_user.create_time'));
        //$grid->column('update_time',  ___('table.zy_user.update_time'));
        //$grid->column('remember_token',  ___('table.zy_user.remember_token'));


        $grid->filter(function($filter){

            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            // 在这里添加字段过滤器
            $filter->like('nickname', '昵称')->placeholder('用户昵称模糊搜索');
            $filter->equal('phone', '手机号');
            $filter->in('gender', '性别')->checkbox([
                '1'    => '男',
                '2'    => '女',
            ]);
            $filter->equal('authentication','认证状态')->radio([
                1 => 'YES',
                0 => 'NO',
            ]);
            //$filter->between('birthday', '年龄范围')->datetime(['format'=>'YYYY-MM-DD HH:mm:ss']);
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(User::findOrFail($id));

        $show->field('id',  ___('table.zy_user.Id'));
        $show->field('phone',  ___('table.zy_user.Phone'));
        //$show->field('password',  ___('table.zy_user.Password'));
        $show->field('icon',  ___('table.zy_user.Icon'))->image();
        $show->field('nickname',  ___('table.zy_user.Nickname'));
        $show->field('remark',  ___('table.zy_user.Remark'));
        $show->field('gender',  ___('table.zy_user.Gender'));
        $show->field('birthday',  ___('table.zy_user.Birthday'));
        $show->field('email',  ___('table.zy_user.Email'));
        $show->field('province_id',  ___('table.zy_user.Province id'));
        $show->field('city_id',  ___('table.zy_user.City id'));
        $show->field('district_id',  ___('table.zy_user.District id'));
        $show->field('name',  ___('table.zy_user.Name'));
        $show->field('authentication',  ___('table.zy_user.Authentication'));
        $show->field('integral',  ___('table.zy_user.Integral'));
        $show->field('balance',  ___('table.zy_user.Balance'));
        $show->field('status',  ___('table.zy_user.Status'));
        $show->field('login_time',  ___('table.zy_user.Login time'));
        $show->field('create_time',  ___('table.zy_user.Create time'));
        $show->field('update_time',  ___('table.zy_user.Update time'));
        //$show->field('remember_token',  ___('table.zy_user.Remember token'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new User());

        $form->mobile('phone',  ___('table.zy_user.Phone'))->creationRules('unique:user');
        $form->password('password',  ___('table.zy_user.Password'));

        $form->imageExt('icon', ___('table.zy_user_media.media_link'))
            ->move('user', true)
            ->type('image')->required();

        $form->text('nickname',  ___('table.zy_user.Nickname'))->required()->creationRules('unique:user');
        $form->email('email',___('table.zy_user.Email'))->required()->creationRules('unique:user');
        $form->text('remark',  ___('table.zy_user.Remark'));
        $form->radio('gender',  ___('table.zy_user.Gender'))
            ->options([1=>'男', 2=>'女'])->default(1);
        $form->date('birthday',  ___('table.zy_user.Birthday'))->required();
        $form->select('province_id', ___('table.zy_user.Province id'))
            ->options('/admin/ajax/region?q=0')
            ->load('city_id','/admin/ajax/region')->required();
        $form->select('city_id', ___('table.zy_user.City id'))
            ->load('district_id','/admin/ajax/region')->required();
        $form->select('district_id', ___('table.zy_user.District id'))
            ->load('car_id','/admin/ajax/secureCar')->required();
        $form->text('name',  ___('table.zy_user.Name'))->required();
        $form->switch('authentication',  ___('table.zy_user.Authentication'));
        $form->currency('integral',  ___('table.zy_user.Integral'));
        $form->currency('balance',  ___('table.zy_user.Balance'));
        $form->switch('status',  ___('table.zy_user.Status'));
        $form->datetime('login_time',  ___('table.zy_user.Login time'))->format('YYYY-MM-DD HH:mm:ss');

/*        $form->number('create_time',  ___('table.zy_user.Create time'));
        $form->number('update_time',  ___('table.zy_user.Update time'));
        $form->text('remember_token',  ___('table.zy_user.Remember token'));*/

        $form->saving(function (Form $form) {
            // 编辑. 如果密码为空，则表示不更新
            if ($form->model()->id && !$form->password) {
                $form->password = $form->model()->password;
            }else{
                // 新创建用户或者编辑改了密码
                $form->password = Hash::make($form->password);
            }
        });
        return $form;
    }


    public function load(Request $request)
    {
        $q = $request->get('q');
        return User::where('name', 'like', "%$q%")->paginate(null, ['id', 'name as text', 'nickname']);
    }
}
